$(function(){
    $("#uploadForm").submit(upload);
    $("#updatePassword").submit(update);
});

function update(){
    var passowrd = $("input[name='password']").val();
    var newpassword = $("input[name='repassword']").val();
    var cpassowrd = $("input[name='cpassword']").val();
    // var formData = new FormData();
    // formData.append('passowrd',passowrd);
    // formData.append('repassword',newpassword);
    // formData.append('cpassword',cpassowrd);
    $.ajax({
        url: CONTEXT_PATH+"/user/repassword?password="+passowrd+"&repassword="+newpassword+"&cpassword="+cpassowrd,
        method: "post",
        processData: false,
        contentType: false,
        cache: false,
        success: function(data) {
            alert(data.msg);
        }
    });
    return false;
}

function upload() {
    var accessid = $("input[name='accessid']").val();
    var policy = $("input[name='policy']").val();
    var signature = $("input[name='signature']").val();
    var dir = $("input[name='dir']").val();
    var host = $("input[name='host']").val();
    var expire = $("input[name='expire']").val();
    var key = $("input[name='key']").val();

    var items = {
        "accessid": accessid,
        "host": host,
        "policy": policy,
        "signature": signature,
        "expire": expire,
        "dir": dir,
        "key": key
    };

    var formData = new FormData();
    formData.append('OSSAccessKeyId',items.accessid);
    formData.append('policy',items.policy);
    formData.append('Signature',items.signature);
    // formData.append('x-oss-forbid-overwrite',true);
    formData.append('success_action_status',200);
    formData.append('key',items.dir+items.key+".png");
    formData.append('file',$('#head-image')[0].files[0]);

    $.ajax({
        url: items.host,
        method: "post",
        processData: false,
        contentType: false,
        cache: false,
        data: formData,
        success: function(data) {
            // 更新头像访问路径
            $.post(
                CONTEXT_PATH + "/user/header/url",
                {"fileName":items.key},
                function(data) {
                    data = $.parseJSON(data);
                    if(data.code == 0) {
                        window.location.reload();
                    } else {
                        alert(data.msg);
                    }
                }
            );
        }
    });
    return false;
}